package com.ld.rightsSystem.controller;

import com.ld.rightsSystem.Enum.ResponseEnum;
import com.ld.rightsSystem.dto.LoginDTO;
import com.ld.rightsSystem.mapper.TUserRepository;
import com.ld.rightsSystem.pojo.TPermission;
import com.ld.rightsSystem.service.inf.TPermissionService;
import com.ld.rightsSystem.utils.JwtUtil;
import com.ld.rightsSystem.utils.Utils;
import com.ld.rightsSystem.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class TLoginController {

    @Autowired
    private TUserRepository tUserRepository;

    @Autowired
    private TPermissionService tPermissionService;

    @PostMapping("/login")
    public R login(@RequestBody LoginDTO loginDTO){
        String tUserPasswordByUserName = tUserRepository.findTUserPasswordByUserName(loginDTO.getUsername());
        if(tUserPasswordByUserName == null) {
            return new R().error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR,null);
        }
        String passwd = Utils.passwordEncrypt(loginDTO.getPassword());
        if(!passwd.equals(tUserPasswordByUserName)){
            return new R().error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR,null);
        }

        JwtUtil jwtUtil = new JwtUtil();
        String token = jwtUtil.generateToken(loginDTO.getUsername());
        Map<String,Object> result = new HashMap<>();
        result.put("token",token);
        return new R().OK(result);
    }

}
